package com.s3.khospitalds.controller;

import com.s3.khospitalds.common.R;
import com.s3.khospitalds.entity.PatientPresiptionList;
import com.s3.khospitalds.entity.Patientlist;
import com.s3.khospitalds.entity.Prescription;
import com.s3.khospitalds.service.PatientlistService;
import com.s3.khospitalds.service.impl.PatientlistServiceImpl;
import com.s3.khospitalds.service.impl.PrescriptionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: xw
 * @CreateTime: 2025/2/25
 * @Description: 处理药方表
 * @Version: 1.0
 */
@RestController
@CrossOrigin(origins = "*")
public class PatientlistController {
    @Autowired
    PatientlistServiceImpl patientlistService;
    @Autowired
    PrescriptionServiceImpl patientlistServiceImpl;
    @Autowired
    PrescriptionServiceImpl prescriptionService;

    @GetMapping("/getPatientList")
    public R getPatientList(){
        return new R().addData("list",patientlistService.list());
    }
    @GetMapping("/selectWaitingNumber0")
    public R selectWaitingNumber0(){
        R r = new R();
        List<Patientlist> list = patientlistService.selectWaitingNumber0();//查询出所有的处理药方表信息
        for (int i = 0; i < list.size(); i++) {//循环遍历
            PatientPresiptionList plist = new PatientPresiptionList();//创建对象
            Patientlist p = list.get(i);//获取单个处理药方表信息
            List<Prescription> prescriptionByPatientNumber = patientlistServiceImpl.findPrescriptionByPatientNumber(p.getNumber());//根据处理药方表信息的编号查询出该编号的所有药品
            //将值存入实体类
            plist.setId(p.getId());
            plist.setDate(p.getDate());
            plist.setNumber(p.getNumber());
            plist.setName(p.getName());
            plist.setSex(p.getSex());
            plist.setRegion(p.getRegion());
            plist.setFamilyName(p.getFamilyName());
            plist.setPhone(p.getPhone());
            plist.setRelation(p.getRelation());
            plist.setIllHistory(p.getIllHistory());
            plist.setIdCard(p.getIdCard());
            plist.setAge(p.getAge());
            plist.setSymptoms(p.getSymptoms());
            plist.setDisease(p.getDisease());
            plist.setPosition(p.getPosition());
            plist.setAddress(p.getAddress());
            plist.setDoctor(p.getDoctor());
            plist.setPharmacist(p.getPharmacist());
            plist.setPatientcountList(prescriptionByPatientNumber);
            plist.setWaitingNumber(p.getWaitingNumber());
            //将值存入r
            r.addData("list"+i, plist);
        }
        return r;
    }
    @GetMapping("/selectWaitingNumber2")
    public R selectWaitingNumber2(){
        R r = new R();
        List<Patientlist> list = patientlistService.selectWaitingNumber2();//查询出所有的处理药方表信息
        for (int i = 0; i < list.size(); i++) {//循环遍历
            PatientPresiptionList plist = new PatientPresiptionList();//创建对象
            Patientlist p = list.get(i);//获取单个处理药方表信息
            List<Prescription> prescriptionByPatientNumber = patientlistServiceImpl.findPrescriptionByPatientNumber(p.getNumber());//根据处理药方表信息的编号查询出该编号的所有药品
            //将值存入实体类
            plist.setId(p.getId());
            plist.setDate(p.getDate());
            plist.setNumber(p.getNumber());
            plist.setName(p.getName());
            plist.setSex(p.getSex());
            plist.setRegion(p.getRegion());
            plist.setFamilyName(p.getFamilyName());
            plist.setPhone(p.getPhone());
            plist.setRelation(p.getRelation());
            plist.setIllHistory(p.getIllHistory());
            plist.setIdCard(p.getIdCard());
            plist.setAge(p.getAge());
            plist.setSymptoms(p.getSymptoms());
            plist.setDisease(p.getDisease());
            plist.setPosition(p.getPosition());
            plist.setAddress(p.getAddress());
            plist.setDoctor(p.getDoctor());
            plist.setPharmacist(p.getPharmacist());
            plist.setPatientcountList(prescriptionByPatientNumber);
            plist.setWaitingNumber(p.getWaitingNumber());
            //将值存入r
            r.addData("list"+i, plist);
        }
        return r;
    }
    @GetMapping("/getAllPatientList")
    public R getAllPatient() {//查询所有待处理的药方
        return new R().addData("Patient",patientlistService.selectWaitingNumber(1));
    }
    @GetMapping("/getAllPatientList2")
    public R getAllPatientTwo() {//查询所有已出药的药方
        return new R().addData("Patient",patientlistService.selectWaitingNumber(2));
    }
    @GetMapping("/getName/{name}")
    public R getName(@PathVariable String name) {
        return new R().addData("Patient",patientlistService.selectName(name));
    }
    @GetMapping("/getNameAndNumber/{number}/{name}")
    public R getNameAndNumber(@PathVariable("number")String number,@PathVariable("name")String name) {
        R r = new R();
        List<Patientlist> list = patientlistService.selectNameAndNumber(number,name);//查询出所有的处理药方表信息
        for (int i = 0; i < list.size(); i++) {//循环遍历
            PatientPresiptionList plist = new PatientPresiptionList();//创建对象
            Patientlist p = list.get(i);//获取单个处理药方表信息
            List<Prescription> prescriptionByPatientNumber = patientlistServiceImpl.findPrescriptionByPatientNumber(p.getNumber());//根据处理药方表信息的编号查询出该编号的所有药品
            //将值存入实体类
            plist.setId(p.getId());
            plist.setDate(p.getDate());
            plist.setNumber(p.getNumber());
            plist.setName(p.getName());
            plist.setSex(p.getSex());
            plist.setRegion(p.getRegion());
            plist.setFamilyName(p.getFamilyName());
            plist.setPhone(p.getPhone());
            plist.setRelation(p.getRelation());
            plist.setIllHistory(p.getIllHistory());
            plist.setIdCard(p.getIdCard());
            plist.setAge(p.getAge());
            plist.setSymptoms(p.getSymptoms());
            plist.setDisease(p.getDisease());
            plist.setPosition(p.getPosition());
            plist.setAddress(p.getAddress());
            plist.setDoctor(p.getDoctor());
            plist.setPharmacist(p.getPharmacist());
            plist.setPatientcountList(prescriptionByPatientNumber);
            //将值存入r
            r.addData("list"+i, plist);
        }
        return r;
    }

    @GetMapping("/getPatientAndPresiptionList/{num}")
    public R getPatientAndPresiptionList(@PathVariable int num) {
        R r = new R();
        List<Patientlist> list = patientlistService.selectWaitingNumber(num);//查询出所有的处理药方表信息
        for (int i = 0; i < list.size(); i++) {//循环遍历
            PatientPresiptionList plist = new PatientPresiptionList();//创建对象
            Patientlist p = list.get(i);//获取单个处理药方表信息
            List<Prescription> prescriptionByPatientNumber = patientlistServiceImpl.findPrescriptionByPatientNumber(p.getNumber());//根据处理药方表信息的编号查询出该编号的所有药品
            //将值存入实体类
            plist.setId(p.getId());
            plist.setDate(p.getDate());
            plist.setNumber(p.getNumber());
            plist.setName(p.getName());
            plist.setSex(p.getSex());
            plist.setRegion(p.getRegion());
            plist.setFamilyName(p.getFamilyName());
            plist.setPhone(p.getPhone());
            plist.setRelation(p.getRelation());
            plist.setIllHistory(p.getIllHistory());
            plist.setIdCard(p.getIdCard());
            plist.setAge(p.getAge());
            plist.setSymptoms(p.getSymptoms());
            plist.setDisease(p.getDisease());
            plist.setPosition(p.getPosition());
            plist.setAddress(p.getAddress());
            plist.setDoctor(p.getDoctor());
            plist.setPharmacist(p.getPharmacist());
            plist.setPatientcountList(prescriptionByPatientNumber);
            //将值存入r
            r.addData("list"+i, plist);
        }
        return r;
    }
    @PostMapping("/updateWaitingNumber")
    public R updateWaitingNumber(@RequestBody String number) {//根据编号进行驳回
        return new R().addData("number",patientlistService.updateWaitingNumber(number));
    }
    @PostMapping("/updateWaitingNumber2")
    public R updateWaitingNumber2(@RequestBody String number) {//根据编号进行驳回
        return new R().addData("number",patientlistService.updateWaitingNumber2(number));
    }
    @PostMapping("/updatePharmacist/{pharmacist}/{number}")
    public R updatePharmacist(@RequestBody @PathVariable("pharmacist")String pharmacist, @PathVariable("number")String number) {//出药的医生
        return new R().addData("number",patientlistService.updatePharmacist(pharmacist,number));
    }
    @PostMapping("/insertPatientlist")
    public R insertPatientlist(@RequestBody Patientlist patientlist) {
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        // 分别获取年、月、日、时、分、秒
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从 0 开始，需要 +1
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String date = year + "-" + month + "-" + day;//放入date
        String number=year+""+month+""+day+""+hour+""+minute+""+second;//放入number
        patientlist.setDate(date);
        patientlist.setNumber(number);
        String sex = patientlist.getSex();
        if (sex.equals("1")){
            patientlist.setSex("男");
        }else {
            patientlist.setSex("女");
        }
        patientlist.setWaitingNumber("1");
        return new R().addData("list",patientlistService.insertPatientList(patientlist));
    }
    @PostMapping("/updatePatientlist")
    public R updatePatientlist(@RequestBody Patientlist patientlist) {
        return new R().addData("list",patientlistService.updatePatientList(patientlist));
    }
    @PostMapping("/deletePatientList")
    public R deletePatientList(@RequestBody String number) {
        int i1 = prescriptionService.deletePrescriptionByNumber(number);
        int i= patientlistService.deletePatientList(number);
        return new R().addData("list1",i1).addData("list",i);
    }
    @PostMapping("/insertPatientlist2")
    public R insertPatientlist2(@RequestBody Patientlist patientlist) {
        System.out.println("patientlist = " + patientlist);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从 0 开始，需要 +1
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String number=year+""+month+""+day+""+hour+""+minute+""+second;//放入number
        patientlist.setNumber(number);
        int result = patientlistService.insertPatientList2(
                patientlist.getDate(),
                patientlist.getNumber(),
                patientlist.getName(),
                patientlist.getSex(),
                (int) patientlist.getAge(),
                patientlist.getPhone(),
                patientlist.getIdCard(),
                patientlist.getDisease(),
                patientlist.getDoctor(),
                patientlist.getPosition(),
                patientlist.getCopayFee()
        );
        return new R().addData("result", result);
    }
}
